package com.hpin.mlxteacher.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.SimpleResult;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;
import com.hpin.mlxteacher.utils.LogUtil;

/**
 * 忘记密码输入密码页面
 * Created by Aaron on 15/8/3.
 */
public class ResetPswActivity extends BaseActivity {

    private EditText et_new_psw;
    private EditText et_new_psw_repeat;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_input_psw);

        //获得电话号码
        phone = getIntent().getStringExtra("phone");

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.reset_psw));

        et_new_psw = (EditText) findViewById(R.id.et_new_psw);
        et_new_psw_repeat = (EditText) findViewById(R.id.et_new_psw_repeat);

        Button btn_commit = (Button) findViewById(R.id.btn_commit);

        iv_left.setOnClickListener(this);
        btn_commit.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

            case R.id.btn_commit: //完成

                if (CommonUtils.isNull(et_new_psw.getText().toString().trim())) {
                    showToast("请输入新密码");
                    return;
                }

                if (!et_new_psw.getText().toString().trim().equals(et_new_psw_repeat.getText().toString().trim())) {
                    showToast("您两次输入的新密码不一致");
                    return;
                }

                JSONObject j = new JSONObject();
                j.put("phoneNum", phone);
                j.put("pwd", et_new_psw.getText().toString().trim());

                HttpUtil.setPostRequest(Constant.MODIFY, this, Constant.WEB_ROOT + Constant.FORGET,
                        j.toJSONString(), ResetPswActivity.class.getName(), new HttpUtil.SuccessListener() {
                            @Override
                            public void setOnSuccessListener(String str) {

                                try {
                                    SimpleResult result = JSONObject.parseObject(str, SimpleResult.class);

                                    if (result == null) {
                                        showToast(Constant.NODATA);
                                        return;
                                    }

                                    if (result.code == 0) {

                                        Intent intent = new Intent(ResetPswActivity.this, LoginActivity.class);
                                        startActivity(intent);
                                        finish();

                                    } else {
                                        showToast(Constant.showErrorMsg(result.code, ResetPswActivity.this));
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    LogUtil.e("TAG", "解析错误===>" + str);
                                }

                            }
                        }, null);

                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());

    }

}
